package teach.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import teach.aistar.model.entity.*;
import teach.aistar.model.vo.Result;
import teach.aistar.service.IBuyCarsService;
import teach.aistar.service.ICarBandsService;
import teach.aistar.service.ICarCartService;
import teach.aistar.service.ICarStyleService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/carts")
public class CarCartController {
//    @GetMapping("/index01")
//    public String test01(){
//        return "/WEB-INF/orange/about.jsp";
//    }
//
//    @GetMapping("/index02")
//    public String test02(){
//        return "/WEB-INF/orange/buycar.jsp";
//    }
    @Autowired
    private IBuyCarsService iBuyCarsService;

    @Autowired
    private ICarCartService iCarCartService;

    @Autowired
    private ICarBandsService iCarBandsService;

    @Autowired
    private ICarStyleService iCarStyleService;

    @RequestMapping("/content")

    public String select(HttpServletRequest req,HttpSession httpSession)
    {
        User user = (User) httpSession.getAttribute("User");
        System.out.println("USER"+user);
        List<CarCart> carCarts  = iCarCartService.selectUid(user.getUId());
        List<CarBrands> carBrands=iCarBandsService.selectAll();
        List<CarStyle> carStyles=iCarStyleService.findAll();

        req.setAttribute("carStylesList",carStyles);
        req.setAttribute("carBrandsList",carBrands);
        req.setAttribute("carts", carCarts);
       return "/orange/content";

    }
    @RequestMapping("/index1")

    public  String index()
    {
        return "/orange/shoping-cart";
    }


    //删除购物车信息
    @RequestMapping("/deleteCart")
    @ResponseBody
    public Result deleteCart(HttpServletRequest request, HttpSession session) {
       int  id=Integer.valueOf(request.getParameter("id"));
//        System.out.println(id+"aaaaaaaaaaa");
        iCarCartService.deleteById(id);
        return new Result("200");
    }

    //更新购物车数量
    @RequestMapping("/updateCart")
    @ResponseBody
    public Result updateCart(HttpServletRequest request, HttpSession session) {
        int  cid=Integer.valueOf(request.getParameter("cid"));
        int counts=Integer.valueOf(request.getParameter("counts"));
//        System.out.println(id+"aaaaaaaaaaa");
        iCarCartService.updates(cid,counts);
        return new Result("200");
    }

    @PostMapping("/add")
    @ResponseBody
    public Result shop(HttpServletRequest req ,HttpSession session) {


        String pid = req.getParameter("pid");

        if (pid != null) {

            User user = (User) session.getAttribute("User");
            if(user!=null) {


            CarDetail carDetail = iBuyCarsService.findId(Integer.valueOf(pid));

                int uId = user.getUId();
            CarCart cart = new CarCart();
            cart.setCtId(carDetail.getLikes());
            cart.setCaBaseprice(carDetail.getcPrice());
            cart.setCaPrice(carDetail.getcOprice());
            cart.setCaSellprice(carDetail.getcOprice());
            cart.setCaPhoto(carDetail.getcPhoto());
            cart.setCbrId(carDetail.getBrId());
            cart.setCstId(carDetail.getStId());
            cart.setCdesc(carDetail.getcDesc());





                cart.setUserId(uId);
                int x = iCarCartService.addCart(cart);
                if (x > 0)
                    return new Result("200", "添加成功", null);

                return new Result("402", "添加出错", null);
            }
            else
            {

                return new Result("405", "用户未登录，请您先进行登录", null);

            }






//        Result result = new Result("200","添加成功",null);



        } else

            return new Result("400", "添加出错", null);


    }

    }
